package com.ogawa.projectcommon.appdatabase

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
import com.ogawa.projectcommon.bean.*
import com.ogawa.projectcommon.dao.*
import java.util.concurrent.Executors

@Database(
    entities = [EntityUserDevice::class, CollectBean::class, Program8506E::class, DiyBean::class, MusicBean::class,MusicMixCollectBean::class],
    version = 3
)
abstract class AppDatabase : RoomDatabase() {
    abstract fun entityUserDeviceDao(): EntityUserDeviceDao
    abstract fun getCollectDao(): ProgramCollectDao
    abstract fun getProgram8506EDao(): Program8506EDao
    abstract fun getDiyProgramDao(): DiyProgramDao
    abstract fun getMusicDao(): MusicDao
    abstract fun getMixMusicCollectDao(): MusicMixCollectDao

    companion object {
        private var INSTANCE: AppDatabase? = null

        fun get(context: Context): AppDatabase {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(
                    context, AppDatabase::class.java,
                    "AppDatabase"
                )
                    .allowMainThreadQueries()//允许在主线程中查询,如果没有这句，就只能异步调用
                    .addCallback(object : RoomDatabase.Callback() {
                        override fun onCreate(db: SupportSQLiteDatabase) {
                        }
                    })
                    .build()
            }
            return INSTANCE!!
        }

    }
}

private val EXECUTOR = Executors.newSingleThreadExecutor()

fun ioThread(f: () -> Unit) {
    EXECUTOR.execute(f)
}

